<?php
/*
 * Copyright (C) 2006 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 * created Sep 3, 2007
 *
 * @author pablif@gmail.com
 */

include_once('../demo/gb-do-authentication.php');
require_once('../library/gb-items.php');

#define('PROXY', 'http://192.168.128.150:3128');
define('PROXY', null);

$item = new GoogleBaseItem();
$item->setDraftStatus(true);
$item->setAuthor('palbo', 'pablif@gmail.com');
$item->setTitle('chicken recipe');
$item->setDescription('yummy');
$item->setLink('http://200.69.205.154/~pablof/base-demo/dummy/?p=chick+recipe');
$item->addGbaseAttribute('item_type', 'Recipes', 'text');
$item->addGbaseAttribute('cooking_time', '30', 'text');
$item->addGbaseAttribute('main_ingredient', 'chicken', 'text');
$item->addGbaseAttribute('main_ingredient', 'chili peppers', 'text');
$item->addGbaseAttribute('main_ingredient', 'peanuts', 'text');
$item->addGbaseAttribute('spices_used', 'szechuan peppercorn', 'text');
$item->addGbaseAttribute('zen_id', '42', 'text', true);
$item->addGbaseAttribute('cook_technique', 'run over', 'text');
                           
$item2 = new GoogleBaseItem();
$item2->setDraftStatus(true);
$item2->setAuthor('palbo', 'pablif@gmail.com');
$item2->setTitle('spicy chicken');
$item2->setDescription('nice and cool chicken');
$item2->setLink('http://200.69.205.154/~pablof/base-demo/dummy/?p=hous+recipe');
$item2->addGbaseAttribute('item_type', 'Recipes', 'text');
$item2->addGbaseAttribute('cooking_time', '30', 'text');
$item2->addGbaseAttribute('main_ingredient', 'chicken', 'text');
$item2->addGbaseAttribute('main_ingredient', 'chili peppers', 'text');
$item2->addGbaseAttribute('main_ingredient', 'peanuts', 'text');
$item2->addGbaseAttribute('spices_used', 'szechuan peppercorn', 'text');
$item2->addGbaseAttribute('zen_id', '43', 'text', true);
$item2->addGbaseAttribute('cook_technique', 'run over', 'text');

$batch = new GoogleBaseBatchRequest(false);
$batch->addItemForInsert($item);
$batch->addItemForInsert($item2);
?>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?= '<xmp>' . $batch->getXml() . '</xmp>'; ?>

<?php

  echo 'posting batch request ...';
  require_once('../library/gb-http.php');
  $gbhttp = new GoogleBaseHttpRequest(PROXY);
  $response = $batch->post($_SESSION['gb_token'], $gbhttp);
  echo '<br>returned<br><xmp>';
  echo $response->getResponseBody();
  if(!$response->hasErrors()) {
    $pxml = $response->getParsedXmlResponseBody();
    print_r($pxml);
    $item->setId($pxml['xmldata']['atom:feed']['atom:entry'][0]['atom:id']['VALUE']);
    $item->setTitle('sandwich recipe');
    $gbattr = $item->getGbaseAttribute('main_ingredient');
    if(is_object($gbattr[0])) {
      $gbattr[0]->setValue('meat');
    }
  }
  echo '###################################################';
  $batch = new GoogleBaseBatchRequest(false);
  $batch->addItemForUpdate($item);
  $response = $batch->post($_SESSION['gb_token'], $gbhttp);
  if(!$response->hasErrors()) {
    $pxml = $response->getParsedXmlResponseBody();
    print_r($pxml);
  }
  echo '</xmp>';
?>
</html>

